package com.atguigu.gmall.payment.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AliPayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: 李旭
 * @create: 2021-09-14 14:24
 *
 *  支付宝
 **/
@Service
public class AliPayServiceImpl implements AliPayService {


    @Autowired
    private PaymentService paymentService;
    @Autowired
    private AlipayClient alipayClient;

    //支付宝 //调用统一收单下单并支付页面接口
    @Override
    public String submitAliPay(Long orderId) {
        //1:创建支付表    给财务进行使用   帐单计算  一张订单 （N个商品） 付钱一次
        PaymentInfo paymentInfo = paymentService.createPayment(orderId, PaymentType.ALIPAY);
        //2:连接杭州 支付宝
        //统一收单下单并支付页面接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        ////////////////////////
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(paymentInfo.getOutTradeNo());
        model.setTotalAmount(paymentInfo.getTotalAmount().toString());
        model.setSubject(paymentInfo.getSubject());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setBody(paymentInfo.getSubject());
        model.setQrPayMode("1");
        model.setQrcodeWidth(100L);
        request.setBizModel(model);
        //将来一旦付钱  同步通知  异步通知
        //同步通知
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //异步通知
        request.setNotifyUrl(AlipayConfig.notify_payment_url);
        //发出
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }

    //退款
    @Override
    public void refund(String outTradeNo) {
        PaymentInfo paymentInfo = paymentService.getPaymentInfo(outTradeNo);
        //退款
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(outTradeNo);
        model.setRefundAmount(paymentInfo.getTotalAmount().toString());
        request.setBizModel(model);

        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

    }
    //关闭交易
    public void closeTrade(Long orderId){
        PaymentInfo paymentInfo = paymentService.getPaymentInfo(orderId);
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        AlipayTradeCloseModel model = new AlipayTradeCloseModel();
        model.setOutTradeNo(paymentInfo.getOutTradeNo());
        request.setBizModel(model);
        AlipayTradeCloseResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
